

package seller

import (
    "dhfshop/models"
	"strings"
	"errors"
	"time"
	"dhfshop/models/consts"
	"github.com/astaxie/beego/orm"
)

// 提现申请
type CashReq struct {
	CashReqId    int64      `orm:"column(cash_req_id);pk;auto" json:"cash_req_id" form:"cash_req_id"` 
	CreatedAt    time.Time  `orm:"auto_now_add;column(created_at);type(datetime)" json:"-" form:"-"`   /* 创建时间 */
	CreatedAtStr string     `orm:"-" json:"created_at" form:"-"`
	AmountReq    float32    `orm:"column(amount_req);digits(12);decimals(2)" json:"amount_req" form:"amount_req"` /* 申请金额 */
	AlipayNo     string     `orm:"column(alipay_no);size(128);null" json:"alipay_no" form:"alipay_no"`   /* 支付宝账号 */
	Payee        string     `orm:"column(payee);null" json:"payee" form:"payee"`     /* 收款人 */
	PayTime      time.Time  `orm:"column(pay_time);type(datetime);null" json:"-" form:"-"`   //支付时间
    PayTimeStr   string     `orm:"-" json:"pay_time" form:"pay_time"`               /* 输入输出 */
	OuterNo      string     `orm:"column(outer_no);size(190);index" json:"outer_no" form:"outer_no"` /*支付网关的内部交易单号，比如支付宝流水号 */
	Status       string     `orm:"column(status)" json:"status" form:"status"` /* 状态：创建、待审核、待支付、已支付、作废 */
    UpdateTime   int64      `orm:"column(update_time)" json:"update_time" form:"update_time"`     // 更新时间
}

func init() {
    orm.RegisterModel(new(CashReq))   
}

func AddCashReq(m *CashReq) (id int64, err error) {
	o := models.GetDefaultDB()
	id, err = o.Insert(m)
	return
}

func GetCashReqById(id int64) (v *CashReq, err error) {
	o := models.GetDefaultDB()
	v = &CashReq{CashReqId: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}

func GetAllCashReqs(query map[string]string,fields []string,order_by map[string]string,offset int,limit int)([]*CashReq,int64,error){
	o := models.GetDefaultDB()
	qs := o.QueryTable(new(CashReq))
	for k, v := range query {
		k = strings.Replace(k, ".", "__", -1)
		if ok := strings.Contains(v, "|"); ok {
		    valueArr := strings.Split(v, "|")
			qs = qs.Filter(k, valueArr)
		}else{
		    qs = qs.Filter(k, v)
		}	
	}
	cnt, err := qs.Count()
	// order by:
	var sortFields []string
	for k, v := range order_by {
		orderby := ""
		if v == "desc" {
			orderby = "-" + k
		} else if v == "asc" {
			orderby = k
		} else {
			return nil, 0, errors.New("Error: Invalid order. Must be either [asc|desc]")
		}
		sortFields = append(sortFields, orderby)
	}
	qs = qs.OrderBy(sortFields...)
	var lists []*CashReq
	if _, err = qs.Limit(limit, offset).All(&lists, fields...); err == nil {
	    for _, item := range lists {
			item.CreatedAtStr = item.CreatedAt.Format(consts.DATETIME_FORMAT)
			item.PayTimeStr = item.PayTime.Format(consts.DATETIME_FORMAT)
	    }
		return lists, cnt, nil
	}
	return nil, 0, err
}
	
func UpdateCashReqById(m *CashReq, param ...string) (err error) {
	o := models.GetDefaultDB()
	v := CashReq{CashReqId: m.CashReqId}
	if err = o.Read(&v); err == nil {
		if _, err = o.Update(m, param...); err == nil {
			return nil
		}
	}
	return
}

func DeleteCashReq(id int64) (err error) {
	o := models.GetDefaultDB()
	v := CashReq{CashReqId: id}
	if err = o.Read(&v); err == nil {
		if _, err = o.Delete(&v); err == nil {
			return nil
		}
	}
	return
}




